home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Essentials / rTutors / part2.5 / rTutor.cc next >
Encoding:
C/C++ Source or Header  |  1990-05-18  |  9.2 KB  |  240 lines  |  [TEXT/pdos]

  1. /* PROGRAM R.Tutor  */
  2. /* (a home-grown tutorial for writing applications using resources */
  3. /* Part 1 - starting up & shutting down the tools from a ToolStartup */
  4. /*          list kept in a resource */
  5. /* Part 2 - add a menu bar that is kept in a resource */
  6. /* Part 3 - add the event loop so that the menus "come alive" and put up a */
  7. /*          window that is defined in the resource fork */
  8.  
  9. #include <types.h>
  10. #include <INTMATH.h>
  11. #include <MEMORY.h>
  12. #include <MISCTOOL.h>
  13. #include <LOCATOR.h>
  14. #include <DESK.H>
  15. #include <QUICKDRAW.h>
  16. #include <EVENT.h>
  17. #include <CONTROL.h>
  18. #include <WINDOW.h>    
  19. #include <MENU.h>    
  20.  
  21.  
  22. #define  kStartStopID    1L    /* used when starting and shutting down tools */
  23.  
  24. /*---------------------- Menus & Menu Bars ---------------------------*/
  25. #define  kMenuBarID      1L   /* resource ID of the menu bar itself */
  26.  
  27.     /* define all the menu id's */
  28. #define  kAppleMenuID   1000  /* resource ID of the Apple menu */
  29. #define  kFileMenuID    2000  /* resource ID of the File menu */
  30. #define  kEditMenuID    3000  /* resource ID of the Edit menu */
  31.  
  32.     /* now, define the menu item id's */
  33. #define   kAboutBoxID   1001  /* resource ID of the About Box menu item */ 
  34.  
  35. #define   kNewItemID    2001  /* resource ID of the New Item in File menu */
  36. #define   kOpenItemID   2002  /* resource ID of the Open item in File menu */ 
  37. #define   kCloseItemID   255  /* the "Close" item */
  38. #define   kSaveItemID   2004  /* the "Save" item */
  39. #define   kSaveAsItemID 2005  /* the "Save As..." item */
  40. #define   kRevertItemID 2006  /* the "Revert to Saved" item */
  41. #define   kPageItemID   2007  /* the "Page Setup..." item */
  42. #define   kPrintItemID  2008  /* the "Print..." item */
  43. #define   kQuitItemID   2009  /* the "Quit" item */
  44.  
  45. #define   kUndoItemID      250  /* the "Undo" item */
  46. #define   kCutItemID       251  /* the "Cut" item */
  47. #define   kCopyItemID      252  /* the "Copy" item */
  48. #define   kPasteItemID     253  /* the "Paste" item */
  49. #define   kClearItemID     254  /* the "Clear" item */
  50. #define   kSelectItemID   3001  /* the "Select All" item */
  51. #define   kShowClipItemID 3002  /* the "Show ClipBoard" item */
  52.  
  53.  
  54. /* declare the constants for our TaskMaster event mask */
  55. #define   kMyTaskMask    0x001FFFFFL  /* this mask is for TaskMaster itself */
  56. #define   kMyEventMask    0xFFFF      /* this mask is passed to GetNextEvent */
  57.  
  58. /* declare the global variable that we'll use with TaskMaster */
  59. WmTaskRec    gMyEvent;
  60.  
  61.     
  62. /* declare all of the global variables that we'll be using */    
  63. unsigned   gMyMemID;    /* holds the ID returned by MMStartup */
  64. Ref       gToolListRef; /* the list of tools used to start and stop the tools */
  65. Boolean   gPunt;        /* TRUE if it's time to quit the app */
  66.  
  67.  
  68. /* ------------------------------------------------------------------------ */
  69. /* the following procedure is responsible for putting up the About box */
  70.  
  71. do_show_about()
  72. {
  73.     /* empty for now - just beep */
  74.     SysBeep();
  75. }
  76.  
  77.  
  78. /* ------------------------------------------------------------------------ */
  79. /* the following procedure is responsible for quitting the app */
  80.  
  81. do_quit_app()
  82. {
  83.   /* we actually just set the "quit" flag and let the real quitting happen */
  84.   /* in the main event loop.  Later, we would add code here to cope with */
  85.   /* closing all open windows, saving their contents, etc. */
  86.     
  87.     gPunt = TRUE;
  88. }
  89.  
  90.  
  91. /* ------------------------------------------------------------------------ */
  92. /* the following procedure is responsible for dealing with items picked */
  93. /* from the menus by the user */
  94.  
  95. do_menu_events()
  96. {
  97.  
  98.  unsigned int   pickedMenuID; /*ID of menu the user just picked an item from */
  99.  unsigned int   pickedItemID; /* ID of item the user just picked */
  100.  
  101.  /* the hi-word of wmTaskData is the menu ID */
  102.  pickedMenuID = HiWord(gMyEvent.wmTaskData);
  103.  
  104.  /* the lo-word of wmTaskData is the item ID */
  105.  pickedItemID = LoWord(gMyEvent.wmTaskData);
  106.     
  107.     switch(pickedItemID) 
  108.     {
  109.      case kAboutBoxID : do_show_about();    /* show the About box */
  110.                           break;
  111.      case kNewItemID    : break;            /* that's all for this item */
  112.      case kOpenItemID   : break;            /* that's all for this item */
  113.      case kCloseItemID  : break;            /* that's all for this item */
  114.      case kSaveItemID   : break;            /* that's all for this item */
  115.      case kSaveAsItemID : break;            /* that's all for this item */
  116.      case kRevertItemID : break;            /* that's all for this item */
  117.      case kPageItemID   : break;            /* that's all for this item */
  118.      case kPrintItemID  : break;            /* that's all for this item */
  119.      case kQuitItemID   : do_quit_app();    /* sets gPunt to TRUE  */
  120.                           break;
  121.      case kUndoItemID   : break;            /* that's all for this item */
  122.      case kCutItemID    : break;            /* that's all for this item */
  123.      case kCopyItemID   : break;            /* that's all for this item */
  124.      case kPasteItemID  : break;            /* that's all for this item */
  125.      case kClearItemID  : break;            /* that's all for this item */
  126.      case kSelectItemID : break;            /* that's all for this item */
  127.      case kShowClipItemID : break;          /* that's all for this item */
  128.         
  129.      default: ; /* always have a default action in case something goes wrong */
  130.  
  131.     } /* end of the "switch" statement */
  132.  
  133.     /* Turn off the highlighting on the menu the user picked the item from. */
  134.     /* Then return to the main event loop to see what we'll do next. */
  135.     HiliteMenu(FALSE,pickedMenuID);
  136. }
  137.  
  138.  
  139. /* ------------------------------------------------------------------------ */
  140. /* the following procedure installs the menu bar from a resource */
  141.  
  142. do_make_menus()
  143.    MenuBarRecHndl my_mbar_hndl;
  144.    word menu_bar_height; 
  145.   { 
  146.  
  147.    /* the next three calls are ALL required to bring the menu bar in from */
  148.    /* the resource fork AND make it the current system menu bar.  For */
  149.    /* details, see the IIGS Toolbox Reference, under NewMenuBar2  */
  150.   my_mbar_hndl = NewMenuBar2(refIsResource, kMenuBarID, nil); 
  151.   SetSysBar(my_mbar_hndl); 
  152.   SetMenuBar(nil);
  153.  
  154.   /* now, add NDA's, adjust the sizes of the menus, and draw the menu bar */
  155.   /* kAppleMenuID used to be defined as a long and had to be cast to a */
  156.   /* word here.  It's been redefined to be only a word in size, so the */
  157.   /* casting is no longer needed. */
  158.   FixAppleMenu(kAppleMenuID); /* adds NDA's */
  159.   menu_bar_height =  FixMenuBar(); /* adjust the sizes */
  160.   DrawMenuBar();  /* draw the new menu bar and enjoy! */ 
  161.   }
  162. }
  163.  
  164. /* ------------------------------------------------------------------------ */
  165. /* the following procedure is responsible for starting the tools (using the */
  166. /* list in a resource) if the SartupTools call fails, then gPunt will */
  167. /* contain "TRUE", so we can abort the app the global variable for this app's */
  168. /* memory id is acquired here as well. */
  169.  
  170. do_init_rom()
  171. {
  172.     gMyMemID = _ownerid; /* find out our memory id & save it for later */
  173.  
  174.     /* crank 'em up! - make sure that kStartStopID is defined as a long!!! */
  175.     gToolListRef = StartUpTools(gMyMemID,refIsResource,kStartStopID);
  176.     
  177.     if (_toolErr == noError)
  178.       {      /* there was no error, so the app can continue starting up */
  179.         gPunt = FALSE;
  180.       }
  181.     else
  182.       {     /* something went wrong, so set gPunt to indicate the failure */
  183.         gPunt = TRUE;
  184.       }
  185. }
  186.  
  187.  
  188.  
  189. /* ------------------------------------------------------------------------ */
  190. /* the following procedure is the main event loop. It runs until gPunt is */
  191. /* set to TRUE by the user picking "Quit" from the File menu. */
  192.  
  193. do_main_event()
  194. {
  195.   word  myTaskCode;  /* used to hold the task codes returned by Task Master */
  196.  
  197.   /* tell Task Master which events it can do */
  198.   /* we're lazy, so let it handle everything possible! */
  199.   gMyEvent.wmTaskMask = kMyTaskMask;
  200.  
  201.   while(!gPunt)  /* keep calling Task Master until "Quit" has been selected */
  202.   {
  203.     myTaskCode = TaskMaster(kMyEventMask, &gMyEvent);
  204.     switch(myTaskCode) 
  205.       {
  206.         case wInGoAway:
  207.            break;
  208.         case wInSpecial:
  209.         case wInMenuBar:
  210.            do_menu_events();
  211.             break;
  212.         default:  break; /* always have a default */
  213.       }    /* end of the switch statement */        
  214.     } /* end of the "while" */
  215. } /* end of do_main_event */
  216.  
  217.  
  218. /* ------------------------------------------------------------------------ */
  219. /* the following procedure is the main application itself. */
  220. /* don't forget to add the code that will check the message center to see if */
  221. /* this app was launched by clicking on its icon or by clicking on a data */
  222. /* file created by this app.  If the data file was clicked on, then grab its */
  223. /* name out of the message center and open it instead of opening the */
  224. /* untitled window. */
  225.  
  226. main()
  227. {    
  228.     do_init_rom();    /* get my memory id, start the tools, & set gPunt */
  229.     if (gPunt == FALSE)
  230.       {
  231.         do_make_menus();  /* insert the menu bar */
  232.         InitCursor();     /* this changes the cursor to the "normal" one. */
  233.                           /* It was left as a watch cursor when the tools */
  234.                           /* were started up. */
  235.         do_main_event();
  236.       }
  237.     ShutDownTools(refIsHandle,gToolListRef); /* shut down the tools and quit */
  238. } /* end of main program */
  239.